﻿/*
 *  创建者：    
 *      zlk
 *  创建时间：
 *      2011-7-20
 *  文件说明：
 *      工商局用户的辅助类，包括登录、注销
 */
using System;
using System.Collections.Generic;
using System.Web;
using Sunimage.Spjg.BLL.SysMgr;
using Sunimage.Spjg.Common.SysMgr;
using Sunimage.Spjg.Common;

namespace Sunimage.Spjg.BLL.Helper
{
    /// <summary>
    /// UserHelper 的摘要说明
    /// </summary>
    public class GsjUserHelper
    {
        private static readonly string SESSION_KEY = "gsjUser";
        private static readonly string LASTURL_KEY = "gsjUserlastUrl";
        private static readonly UserBl userBl = new UserBl();
        private static readonly RoleBl roleBl = new RoleBl();

        private GsjUserHelper() { }

        #region user

        public static string Permissions
        {
            get
            {
                if (IsLogin())
                {
                    return User.Permissions;
                }
                else
                {
                    ReLogin();
                }
                return null;
            }
        }

        public static string UserName
        {
            get
            {
                if (IsLogin())
                {
                    return User.UserName;
                }
                else
                {
                    ReLogin();
                }
                return null;
            }
        }

        public static string RealName
        {
            get
            {
                if (IsLogin())
                {
                    return User.RealName;
                }
                else
                {
                    ReLogin();
                }
                return null;
            }
        }

        public static RoleType Role
        {
            get
            {
                if (IsLogin())
                {
                    return User.Role;
                }
                else
                {
                    ReLogin();
                }
                return RoleType.User;
            }
        }

        public static string NickName
        {
            get
            {
                if (IsLogin())
                {
                    return User.NickName;
                }
                else
                {
                    ReLogin();
                }
                return null;
            }
        }

        public static int UserId
        {
            get
            {
                if (IsLogin())
                {
                    return User.Id;
                }
                else
                {
                    ReLogin();
                }
                return -1;
            }
        }

        public static int GsjId
        {
            get
            {
                if (IsLogin())
                {
                    return User.GsjId;
                }
                else
                {
                    ReLogin();
                }
                return -1;
            }
        }

        #endregion

        #region Log in / out

        public static bool Login(string username, string password)
        {
            SessionGsjUser sUser = LoginPrivate(username, password);

            if (sUser != null)
            {
                //UserOpLogHelper.LogGsjUserOp(UserOpEnum.Login, string.Format("用户{0}登录", username));
                return true;
            }
            else
                return false;
        }

        public static void LoginAnonymous()
        {
            //List<Program> viewList = new ProgramBl().GetProgramNonTP();
            //foreach (Program prog in viewList)
            //{
            //    prog.Title = null;      // 节省空间
            //}

            //SessionUser sUser = new SessionUser("Anonymous", "Anonymous",
            //    viewList, new List<Program>(), new List<Program>(), new List<Program>());

            //sUser.NavCfg = NavCfgMgr.GetUserNavCfg(new string[] {"1"});

            //User = sUser;
        }

        private static SessionGsjUser LoginPrivate(string username, string password)
        {
            // GsjUserBl userBl = new GsjUserBl();
            User user = userBl.Login(username, password);
            if (user != null)
            {
                SessionGsjUser sUser = new SessionGsjUser()
                                        {
                                            Id = user.Id,
                                            UserName = user.Username,
                                            NickName = user.NickName,
                                            Role = user.Role
                                        };
                sUser.Permissions = roleBl.GetRole((int)user.Role).Permissions;
                User = sUser;
                return sUser;
            }
            else
                return null;
        }

        public static void Logout()
        {
            HttpContext.Current.Session.Clear();
            System.Web.Security.FormsAuthentication.SignOut();
            HttpContext.Current.Response.Redirect(SystemConfiguration.GetLoginPage() + "?r=f");
        }

        public static void ReLogin()
        {
            HttpContext.Current.Session[LASTURL_KEY] = HttpContext.Current.Request.RawUrl;
            HttpContext.Current.Response.Redirect(SystemConfiguration.GetLoginPage());
        }

        public static void ReLogin(string lastUrl)
        {
            HttpContext.Current.Session[LASTURL_KEY] = lastUrl;
            HttpContext.Current.Response.Redirect(SystemConfiguration.GetLoginPage());
        }

        public static bool IsLogin()
        {
            return (HttpContext.Current.Session[SESSION_KEY] != null);
        }

        public static void CheckLogin()
        {
            if (!IsLogin())
            {
                HttpContext.Current.Session[LASTURL_KEY] = HttpContext.Current.Request.RawUrl;
                HttpContext.Current.Response.Redirect(SystemConfiguration.GetLoginPage());
            }
        }

        private static SessionGsjUser User
        {
            get { return (SessionGsjUser)HttpContext.Current.Session[SESSION_KEY]; }
            set { HttpContext.Current.Session[SESSION_KEY] = value; }
        }

        #endregion


        #region SessionUser
        /// <summary>
        /// 在Session中保存的工商局用户信息
        /// </summary>
        private class SessionGsjUser
        {
            public int Id { get; set; }

            public string Permissions { get; set; }

            public string UserName { get; set; }

            public string RealName { get; set; }

            public string NickName { get; set; }

            public int GsjId { get; set; }

            public RoleType Role { get; set; }
        }

        #endregion



    }
}
